<template>
	<view class="system">
		<fui-list>

			<fui-list-cell arrow @click="showdialog">
				<text>配置系统邮箱</text>
				<!-- 				<text class="">{{input_email}}</text> -->
			</fui-list-cell>
		</fui-list>


		<fui-dialog :show="show" title="" maskClosable :buttons="buttons" @click="onClick">
			<fui-input placeholder="请输入邮箱账号" required v-model="email" @blur="check_input"></fui-input>
			<span style="color: red;">{{hint}}</span>
		</fui-dialog>


		<view class="system_add_btn">
			<fui-button background="#fff" color="#465CFF" borderColor="#465CFF" radius="0"
				@click="system_add_email">修改系统配置信息</fui-button>
		</view>

	</view>
</template>

<script>
	import {
		httpRequest
	} from '../../utils/request.js'
	export default {
		data() {
			return {
				show: false,
				// input_email: "",
				hint: "",
				buttons: [{
					//按钮文本
					text: '取消',
					//按钮字体颜色，可选
					color: '#333'
				}, {
					//按钮文本
					text: '确定',
					//按钮字体颜色
					color: '#465CFF'
				}],
				email: ""

			}
		},
		methods: {
			showdialog() {
				// for (var i = 0; i < this.$store.state.systemparameter.length; i++) {
				// 	this.input_email = this.$store.state.systemparameter[i].email.mail
				// }
				this.show = true
			},

			/* 添加邮箱请求 */
			onClick(e) {
				this.hint = ""

				const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
				if (emailRegex.test(this.email)) {
					this.hint = ""
					this.show = false
				} else {
					this.hint = "邮箱格式错误"
					this.show = true
				}


			},
			// /* 邮箱表单校验 */
			check_input(e) {
				if (e.detail.value == "") {
					this.hint = "请输入邮箱地址"

				} else {
					const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
					if (emailRegex.test(e.detail.value)) {
						this.hint = ""
					} else {
						this.hint = "邮箱格式错误"
						this.input_email = ""
					}

				}
			},
			system_add_email() {

				let query = {
					"email": this.input_email
				}
				httpRequest("/api/system/system_parameter_email", "post", query).then((res) => {
					uni.showToast({
						title: "系统配置成功"
					})
					let system = [{
							'email': {
								"user_id": res.UserID,
								'email': this.input_email
							}
						}

					]

					this.$store.commit("SystemParameter", system)
					this.input_email = ""

				}).catch(err => {

					uni.showToast({
						icon: 'error',
						title: err,
						position: 'center',
						duration: 1500
					})
				})
			},
			// 加载所有系统配置信息
			get_system_all() {
				httpRequest("/api/system/get_all_system", "get").then((res) => {
					let system = []
					for (let i = 0; i < res.length; i++) {
						system = [{
							'email': {
								'id': res[i].id,
								"user_id": res[i].user_id,
								'email': res[i].email,
								'created_at': res[i].created_at,
								'update_at': res[i].updated_at
							}
						}]
					}
					this.$store.commit("SystemParameter", system)
					for (var i = 0; i < this.$store.state.systemparameter.length; i++) {

						this.email = this.$store.state.systemparameter[i].email.email

					}
				})

			}
		},
		onShow() {
			this.get_system_all()
		},

	}
</script>

<style scoped>
	.system {
		width: 98%;
		margin: 0 auto;
		position: relative;
	}

	.system_add_btn {
		width: 98%;
		position: fixed;
		bottom: 200rpx;

	}
</style>